// pages/regSetPad/regSetPad.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:'',
    passwordType: 'password',
    password:'',
    showTip:false,
    userInfo: '',
    isSetNewPad:false,//"true"：找回密码设置新密码页面
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      phone: options.phone
    })
    if (options.isSetNewPad != undefined && options.isSetNewPad=='true'){
      this.setData({ isSetNewPad:options.isSetNewPad}); 
      this.getUserInfo();
    }
    if (wx.getStorageSync('userInfo') != '') {
      this.setData({
        userInfo: JSON.parse(wx.getStorageSync('userInfo'))
      })
    }
    wx.setNavigationBarTitle({
      title: options.navTitle
    })
    
  },
  // 查询用户信息
  getUserInfo: function(){
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyUserInfo/getUserInfoDetailByPhone',
      data: {
        phone: _this.data.phone
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if(res.data.success){
          _this.setData({
            userInfo: res.data.data
          })
        }
      }
    })
  },
  showPad:function(){
    if (this.data.passwordType == 'password') {
      this.setData({
        passwordType: 'text'
      })
    } else {
      this.setData({
        passwordType: 'password'
      })
    }
  },
  changeInput:function(e){
    this.setData({
      password:e.detail.value
    })
  },
  complete:function(){
    var reg =/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,20}$/;
    var that=this;
    if (!reg.test(this.data.password)) {
      this.setData({
        showTip:true
      })
      setTimeout(function(){
        that.setData({
          showTip: false
        })
      },2000)
    }else{
      if (this.data.isSetNewPad=='true'){
        wx.request({
          url: app.globalData.httpApiCar + 'tGyUserInfo/update',
          data: {
            id: that.data.userInfo.id,
            password: that.data.password
          },
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success: function (res) {
            if (res.data.success) {
              wx.redirectTo({
                url: '/pages/regsuccess/regsuccess?btnText=去登录&showText=找回密码成功，您可以'
              })
              wx.setStorage({
                key: "url",
                data: "/pages/login/login"
              })
            }
            else {
              wx.showToast({
                title: res.data.error,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
      else{
        wx.request({
          url: app.globalData.httpApiCar + 'tGyUserInfo/doRegister',
          // mothod: 'GET',
          data: {
            phone: that.data.phone,
            password: that.data.password
          },
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success: function (res) {
            if(res.data.success){
              wx.redirectTo({
                url: '/pages/regsuccess/regsuccess?btnText=完善信息&showText=恭喜您注册成功，您可以',
              })
              wx.setStorage({
                key: "url",
                data: "/pages/perfectInfo/perfectInfo?isHome=true"
              })
              app.globalData.userInfo = res.data.data;
              wx.setStorageSync('userInfo', JSON.stringify(res.data.data))
            }
            else{
              wx.showToast({
                title: res.data.error,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})